package com.example;

import com.example.config.AppConfig;
import com.example.service.OrderService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MainApp {
    public static void main(String[] args) {
        System.out.println("=== 开始测试 Spring AOP ===");

        try {
            // 创建Spring容器
            AnnotationConfigApplicationContext context =
                    new AnnotationConfigApplicationContext(AppConfig.class);

            System.out.println("✅ Spring容器启动成功！");

            // 获取OrderService Bean
            OrderService orderService = context.getBean(OrderService.class);
            System.out.println("✅ OrderService Bean获取成功！");

            // 测试方法
            System.out.println("\n1. 测试find方法:");
            int result = orderService.find("iPhone");
            System.out.println("查询结果: " + result);

            System.out.println("\n2. 测试add方法:");
            try {
                orderService.add("MacBook", 1);
            } catch (Exception e) {
                System.out.println("add方法异常: " + e.getMessage());
            }

            System.out.println("\n3. 测试delete方法:");
            try {
                orderService.delete("iPad");
            } catch (Exception e) {
                System.out.println("delete方法异常: " + e.getMessage());
            }

            context.close();
            System.out.println("\n🎉 测试完成！");

        } catch (Exception e) {
            System.out.println("❌ 程序运行失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
}